package com.bw.controller;

import com.bw.entity.*;
import com.bw.mapper.HouseMapper;
import com.bw.service.HouseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.formula.functions.T;
import org.spring.kun.common.TokenUtils;
import org.spring.kun.common.UploadPicUtils;
import org.spring.kun.common.ZzyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@Controller
public class ListController {

    @Autowired
    HouseMapper houseMapper;
    @Autowired
    HouseService houseService;

    @RequestMapping("/list.do")
    public String list(HouseQuery query, Integer pageNum, HttpServletRequest request, Model model) {
        // 获取当前的登录用户
        User userFromRequest = TokenUtils.getLoginUserFromRequest(request, User.class);
        model.addAttribute("nickName", userFromRequest.getNickName());
        model.addAttribute("avatarUrl", userFromRequest.getAvatarUrl());
        // 1. 下拉框
        List<Dict> provinceList = houseMapper.listProvince();
        model.addAttribute("provinceList", provinceList);
        // 不加载城市下拉框  （这个数据需要通过 省份下拉框 onchange触发）
        //2.分页查询
        PageHelper.startPage(pageNum == null ? 1 : pageNum, 10);
        List<HouseInfo> list = houseMapper.selectList(query);
        PageInfo<HouseInfo> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo", pageInfo);
        return "list";
    }


    @RequestMapping("/getCity.do")
    @ResponseBody
    public List<Dict> getCity(Integer provinceId) {

        return houseMapper.getCity(provinceId);
    }

    @RequestMapping("/addPage.do")
    public String addPage(Model model) {
        // 1. 省份下拉框
        List<Dict> provinceList = houseMapper.listProvince();
        model.addAttribute("provinceList", provinceList);
        return "add";
    }


    @RequestMapping("/add.do")
    public String add(@Valid HouseAdd houseAdd, BindingResult result, Model model,HttpServletRequest request) {


        if (result.hasErrors()){
            model.addAttribute("error",result.getAllErrors().get(0).getDefaultMessage());
            // 1. 省份下拉框
            List<Dict> provinceList = houseMapper.listProvince();
            model.addAttribute("provinceList", provinceList);
            return "add";
        }

        //楼盘名称不超过10个字符
        String name = houseAdd.getName();
        if (name.length() > 10){
            // 1. 省份下拉框
            List<Dict> provinceList = houseMapper.listProvince();
            model.addAttribute("provinceList", provinceList);
            return "add";
        }
        // 处理图片 得到图片访问地址
        String url = UploadPicUtils.upload(houseAdd.getPicFile(), request);
        // 把图片地址存入数据库
        houseAdd.setPicUrl(url);

        houseService.addHouse(houseAdd);
        //发送短信给管理员用户(在什么时间添加了楼盘信息)
        ZzyUtils.sendSms("17611221735","添加了楼盘，时间是" + System.currentTimeMillis(),5);
        return "redirect:list.do";
    }
}
